package cn.itcast.bos.service.delivery.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.delivery.IPromotionRepository;
import cn.itcast.bos.domain.delivery.Promotion;
import cn.itcast.bos.domain.pageBean.PageBean;
import cn.itcast.bos.service.delivery.IPromotionService;

@Service
@Transactional
public class PromotionServiceImpl implements IPromotionService {
	@Autowired
	private IPromotionRepository promotionRepository;
	
	@Override
	public void save(Promotion model) {
		promotionRepository.save(model);
	}

	@Override
	public Page<Promotion> findPageData(Pageable pageable) {
		return promotionRepository.findAll(pageable);
	}

	@Override
	public PageBean<Promotion> findPageDate(int page, int rows) {
		Pageable pageable = new PageRequest(page - 1, rows);
		Page<Promotion> pageData = promotionRepository.findAll(pageable);
		
		//封装到PageBean中
		PageBean<Promotion> pb = new PageBean<>();
		pb.setTotalCount(pageData.getTotalElements());
		pb.setPageData(pageData.getContent());
		
		return pb;
	}

	@Override
	public Promotion findById(Integer id) {
		return promotionRepository.findOne(id);
	}
}
